package com.hokook.delayed;

import com.hokook.entity.PlainTask;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

@Slf4j
public class DelayedPlainTask implements Delayed {

    /**
     *
     */
    @Getter
    private PlainTask plainTask;

    /**
     *
     */
    @Getter
    private long id;

    private long interval;

    /**
     *
     * @param plainTask
     * @param id
     * @param interval
     */
    public DelayedPlainTask(PlainTask plainTask, long id, long interval) {
        this.plainTask = plainTask;
        this.id = id;
        this.interval = System.currentTimeMillis() + interval;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(interval - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed delayed) {
        DelayedPlainTask plainTask = (DelayedPlainTask) delayed;
        return this.id > plainTask.getId() ? 1
                : (this.id < plainTask.getId() ? -1 : 0);
    }
}
